package com.oa.modules.approvalsec1.service.impl;

import com.aliyuncs.utils.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.oa.common.utils.*;
import com.oa.modules.approvalsec1.dao.ArchivesDao;
import com.oa.modules.approvalsec1.entity.AppointmentMatterEntity;
import com.oa.modules.approvalsec1.entity.ArchivesEntity;
import com.oa.modules.approvalsec1.service.ArchivesService;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author: Hankaicheng
 * @Date: 2022/3/25 10:34
 */
@Service
public class ArchivesServiceImpl extends ServiceImpl<ArchivesDao, ArchivesEntity> implements ArchivesService {

    @Override
    public PageUtils queryPage(Map<String, Object> params ) {
        IPage page= AppUtils.getPage(new Query<ArchivesEntity>(),  params);
        return AppUtils.setPage(page, baseMapper.listCount(params), baseMapper.list(page, params));
    }

    @Override
    public List<ArchivesEntity> allById(String id){
        List<ArchivesEntity> archivesEntities = baseMapper.queryAll(id);
        return archivesEntities;
    }

    @Override
    public void update(ArchivesEntity archivesEntity) {
        baseMapper.updateById(archivesEntity);
    }

    @Override
    public ArchivesEntity getByFileNumber(String id) {
        return baseMapper.selectOne(new QueryWrapper<ArchivesEntity>().eq("file_number", id));
    }

    @Override
    public R queryPage1(Map<String, Object> params) {
        Long pagesize =Long.parseLong(params.get("page").toString());
        Long limit =Long.parseLong(params.get("limit").toString());
        IPage page= AppUtils.getPage(new Query<ArchivesEntity>(),  params);
        QueryWrapper queryWrapper = new QueryWrapper<ArchivesEntity>().likeRight("file_number", params.get("id"));
        if(StringUtils.isNotEmpty((String)params.get("search"))){
            queryWrapper.like("archives_name", params.get("search"));
        }
        Page<ArchivesEntity> userPage = new Page<ArchivesEntity>(pagesize, limit);
        IPage<ArchivesEntity> userIPage = baseMapper.selectPage(userPage , queryWrapper);
        return R.ok().put("page",userIPage);
    }

    @Override
    public R updateByFileNumber(ArchivesEntity entity) {
        int file_number = baseMapper.update(entity, new QueryWrapper<ArchivesEntity>().eq("window_number", entity.getWindowNumber()));
        if (file_number == 1){
            return R.ok("修改成功");
        }
        return R.ok("修改失败");
    }

    @Override
    public R removeByFileNumber(ArchivesEntity entity) {

        int windows_number = baseMapper.delete(new QueryWrapper<ArchivesEntity>().eq("windows_number", entity.getWindowNumber()));
        if (windows_number == 1){
            return R.ok("删除成功");
        }
        return R.ok("删除失败");
    }

    @Override
    public R getByWindowNumber(String id) {
        ArchivesEntity file_number = baseMapper.selectOne(new QueryWrapper<ArchivesEntity>().eq("windows_number", id));
        if (file_number == null){
            return R.ok().put("data",file_number);
        }
        return R.error("没查到对应数据");
    }

    @Override
    public ArchivesEntity getByUId(String id) {
        ArchivesEntity archivesEntity = baseMapper.selectOne(new QueryWrapper<ArchivesEntity>().eq("id", id));
        return archivesEntity;
    }
}
